<?php
if (eregi(".inc.php",$HTTP_SERVER_VARS['PHP_SELF']) || eregi(".inc.php",$_SERVER['PHP_SELF'])) {
	echo "<html>\r\n<head>\r\n<title>Forbidden 403</title>\r\n</head>\r\n<body><h3>Forbidden 403</h3>\r\nThe document you are requesting is forbidden.\r\n</body>\r\n</html>";
	exit;
}

$box_content = new XTemplate ("skins/".$configSkin['value']."/styleTemplates/accountRegister.tpl");

include_once 'includes/forms/RegisterForm1.php';
include_once 'includes/forms/RegisterForm2.php';
include_once 'includes/forms/RegisterForm3.php';
include_once 'includes/models/Account.php';

$form1 = RegisterForm1::getInstance();
$form2 = RegisterForm2::getInstance();
$form3 = RegisterForm3::getInstance();

/* Step 1*/
if(isset($_POST['accTitle']))	//get request from form1
{
	if($form1->isValid($_POST)) //succeed
	{
		$dataForm1 = $form1->getValues(); // get data from FORM1 and save to session
		unset($dataForm1['button']);
		
		$_SESSION['submitform1'] = $dataForm1; //never use $_SESSION['form1'], Linux will misunderstand that is the variable "$form1"
		
		$_SESSION['registerStep'] = '2. Account Access Information';
		$box_content->assign('NOTICE','<div class="notice">Your email address will also allow you to receive customer support email and then retrieve forgotten passwords. Please note that once you create a username, you will not be able to change it.</div>');
		$box_content->assign('FORMTITLE','Enter your Account Access Information');
		$box_content->assign('FORM',$form2->render());	//STEP 2, generate FORM2
		$box_content->assign('SKIN',$configSkin['value']);
		$box_content->parse('page');
		$box_content = $box_content->text('page');
	}
	else // failed
	{
		$form1->populate($_POST);
		
		$_SESSION['registerStep'] = '1. Business Information';
		$box_content->assign('NOTICE','<div class="notice">Please make sure that your information is accurate. This information will be kept confidential and used only for future contact from <b>Shopnmatch.com</b>. You will be able to edit this information later.</div>');
		$box_content->assign('FORMTITLE','Enter your Business Information');
		$box_content->assign('FORM', $form1->render());	//regenerate FORM1
		$box_content->assign("SKIN",$configSkin['value']);
		$box_content->parse('page');
		$box_content =  $box_content->text('page');
	}
}
/* Step 2 */
elseif (isset($_POST['accUsername']))	//get request from form2
{
	if ($form2->isValid($_POST))	//if succeed
	{
		$dataForm2 = $form2->getValues();	//get data from FORM2
		unset($dataForm2['button']);
		unset($dataForm2['accUsername_check']);
		unset($dataForm2['accPassword_check']);
		$dataForm2['accPassword'] = md5($dataForm2['accPassword']);
		$dataForm2['accCreated'] = date("Y-m-d");
		$account = new Account();
		if (!$account->add(array_merge($_SESSION['submitform1'], $dataForm2)))	//combine data in form1 + form2, add to database
		{
			//database error
			$form2->populate($_POST);
			$form2->getElement('button')->setDescription('Your username existed in the database');
		
			$_SESSION['registerStep'] = '2. Account Access Information';
			$box_content->assign('NOTICE','<div class="notice">Your email address will also allow you to receive customer support email and then retrieve forgotten passwords. Please note that once you create a username, you will not be able to change it.</div>');
			$box_content->assign('FORMTITLE','Enter your Account Access Information');
			$box_content->assign('FORM',$form2->render());	//regenerate FORM2
			$box_content->assign('SKIN',$configSkin['value']);
			$box_content->parse('page');
			$box_content = $box_content->text('page');
		}
		else
		{
			//STEP 3, generate FORM3
			$_SESSION['registerStep'] = '3. Confirmation';
			$box_content->assign('NOTICE','<div class="success">Congratulations! Your new Shopnmatch.com Merchant account is created.</div>');
			$box_content->assign('FORMTITLE','Confirmation');
			$box_content->assign('FORM', $form3->render());
			$box_content->assign("SKIN",$configSkin['value']);
			$box_content->assign('RESULT','Registration Successfully. <a href="'.$installDir['value'].'index.php?case=account&act=login"> Click here to login to your account</a>');
			$box_content->parse('page');
			$box_content =  $box_content->text('page');
		}
	}
	else //failed
	{
		$form2->populate($_POST);
		
		$_SESSION['registerStep'] = '2. Account Access Information';
		$box_content->assign('NOTICE','<div class="notice">Your email address will also allow you to receive customer support email and then retrieve forgotten passwords. Please note that once you create a username, you will not be able to change it.</div>');
		$box_content->assign('FORMTITLE','Enter your Account Access Information');
		$box_content->assign('FORM',$form2->render());	//regenerate FORM2
		$box_content->assign('SKIN',$configSkin['value']);
		$box_content->parse('page');
		$box_content = $box_content->text('page');
	}
}
else
{
	$_SESSION['registerStep'] = '1. Business Information';
	$box_content->assign('NOTICE','<div class="notice">Please make sure that your information is accurate. This information will be kept confidential and used only for future contact from <b><b>Shopnmatch.com</b></b>. You will be able to edit this information later.</div>');
	$box_content->assign('FORMTITLE','Enter your Business Information');
	$box_content->assign('FORM', $form1->render());	//generate FORM1
	$box_content->assign("SKIN",$configSkin['value']);
	$box_content->parse('page');
	$box_content =  $box_content->text('page');
}

?>